//
//  WZixunDetailViewController.swift
//  iiatn
//
//  Created by mine on 18/3/29.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD
import WebKit
import Alamofire
import SwiftyJSON

class WZixunDetailViewController: itBaseViewController, WKNavigationDelegate{

    
    var zixunItem : WZiXun?
    
    private var webview = WKWebView()
    
    @IBOutlet var zxdtitle: UILabel!
    
    @IBOutlet var zxdimage: UIImageView!
    
    @IBOutlet var zxdweb: UIWebView!
    
    @IBOutlet var zan: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        zxdtitle.text = "    " + zixunItem!.title!
        zxdimage.frame = CGRect(x: 0, y: 46, width: SCREENW, height: 231)
        zxdtitle.frame = CGRect(x: 0, y: 239, width: SCREENW, height: 38)
        let url = zixunItem!.thumb
        if (url!.isEmpty){
            zxdimage.image = UIImage(named:"default_bg")
        }else{
            zxdimage.kf.setImage(with: URL(string: url!)!, placeholder: UIImage(named: "default_bg"), options: nil, progressBlock: nil)
        }
        
        if whaslogin!{
            let url = BASE_URL + "apin/ios/zx/ifzan/\(wuserid!)/\((zixunItem?.id)!)"
            Alamofire
                .request(url)
                .responseJSON { (response) in
                    guard response.result.isSuccess else {
                        SVProgressHUD.showError(withStatus: "网络连接失败...")
                        return
                    }
                    if let value = response.result.value {
                        let dict = JSON(value)
                        let status = dict["status"].intValue
                        if status == 1 {
                            self.zan.setTitleColor(WlightGreenColor(), for: .normal)
                            self.zan.setImage(UIImage(named:"more_icon_score_21x21_"), for: .normal)
                        }else {
                            self.zan.setTitleColor(WllightGrayColor(), for: .normal)
                            self.zan.setImage(UIImage(named:"hui_more_icon_score_21x21_"), for: .normal)
                        }
                    }
            }
        }else{
            zan.setTitleColor(WllightGrayColor(), for: .normal)
            zan.setImage(UIImage(named:"hui_more_icon_score_21x21_"), for: .normal)
        }
        
        zan.addTarget(self, action: #selector(mzan), for: .touchUpInside)
        
        setweb(mbody: (zixunItem?.content)!)
    }

    func mzan(){
        // bian font color he  image
        if whaslogin!{
            if(zan.currentImage == UIImage(named:"more_icon_score_21x21_")){
                zan.setTitleColor(WllightGrayColor(), for: .normal)
                zan.setImage(UIImage(named:"hui_more_icon_score_21x21_"), for: .normal)
            }else {
                zan.setTitleColor(WlightGreenColor(), for: .normal)
                zan.setImage(UIImage(named:"more_icon_score_21x21_"), for: .normal)
            }
            
            WNetwork.shareNetworkTool.zanZX(user_id: wuserid!, zx_id: zixunItem!.id!)
        }else{
            toLogin()
        }
        
    }
    
    func toLogin(){
        let loginVC = WLoginViewController()
        loginVC.title = "登录"
        let nav = WNavigationController(rootViewController: loginVC)
        present(nav, animated: true, completion: nil)
        // SVProgressHUD.showInfo(withStatus: "暂未登录！")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func setupNav() {
        view.backgroundColor = UIColor.white
    }

    // MARK: - Navigation
    func setweb(mbody: String){
        //创建wkwebview
        SVProgressHUD.showProgress(0.75)
        webview = WKWebView(frame: CGRect(x: 8, y: 315, width: SCREENW-16, height: SCREENH-316))
        webview.navigationDelegate = self
        var html = "<html>"
        html += "<head>"
        html += "<meta name=\"viewport\"content=\"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no,minimal-ui\">"
        html += "<style>img{max-width:" + String(describing: SCREENW-16) + "px !important;}</style>"
        html += "<body>"
        html += mbody
        html += "</body>"
        html += "</head>"
        html += "</html>"
        webview.loadHTMLString(html, baseURL: nil)
        view.addSubview(webview)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        SVProgressHUD.dismiss()
    }

}
